/**
 * Copyright (C) 2016 Hyphenate Inc. All rights reserved.
 * <p>
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * http://www.apache.org/licenses/LICENSE-2.0
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.cnguifang.feigewallet.ui;

import android.app.ProgressDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.text.TextUtils;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.bumptech.glide.Glide;
import com.cnguifang.feigewallet.Constant;
import com.cnguifang.feigewallet.R;
import com.cnguifang.feigewallet.circle.ui.FriendCircle1Activity;
import com.cnguifang.feigewallet.circle.ui.FriendCircleActivity;
import com.cnguifang.feigewallet.eventbus.UpdateAvater;
import com.cnguifang.feigewallet.utils.QiniuUtils;
import com.cnguifang.feigewallet.utils.UserInfoUtils;
import com.mathum.baseapp.callback.UpLoadImgCallback;
import com.mathum.baseapp.data.BaseDialogData;
import com.mathum.baseapp.widget.RecyclerViewDialog;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.ArrayList;
import java.util.List;

import cn.com.mathum.gallerylib.BastiGallery;

import static android.app.Activity.RESULT_OK;

/**
 * settings screen
 */
@SuppressWarnings({"FieldCanBeLocal"})
public class Setting1Fragment extends Fragment implements OnClickListener {

    private RelativeLayout photo;
    private RelativeLayout xiangce;
    private RelativeLayout shoucang;
    private RelativeLayout kabao;
    private RelativeLayout qianbao;
    private RelativeLayout biaoqing;
    private RelativeLayout setting;
    private ImageView imaPhoto;
    private TextView name;


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.em_fragment_conversation_setting1, container, false);
        initLayout(view);
        EventBus.getDefault().register(this);
        return view;
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        EventBus.getDefault().unregister(this);
    }

    private void initLayout(View view) {
        photo = view.findViewById(R.id.photo);
        imaPhoto = view.findViewById(R.id.img_photo);
        name = view.findViewById(R.id.name);
        xiangce = view.findViewById(R.id.xiangce);
        shoucang = view.findViewById(R.id.shoucang);
        qianbao = view.findViewById(R.id.qianbao);
        kabao = view.findViewById(R.id.kabao);
        biaoqing = view.findViewById(R.id.biaoqing);
        setting = view.findViewById(R.id.setting);

        photo.setOnClickListener(this);
        xiangce.setOnClickListener(this);
        shoucang.setOnClickListener(this);
        qianbao.setOnClickListener(this);
        kabao.setOnClickListener(this);
        biaoqing.setOnClickListener(this);
        setting.setOnClickListener(this);

        if (!TextUtils.isEmpty(UserInfoUtils.getAppUserUrl(getActivity()))) {
            Glide.with(this).load(UserInfoUtils.getAppUserUrl(getActivity())).into(imaPhoto);
            name.setText(UserInfoUtils.getUserName(getActivity()));
        }
    }

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        if (savedInstanceState != null && savedInstanceState.getBoolean("isConflict", false))
            return;

    }


    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.photo:
                showSelectPhotoDialog();
                break;
            case R.id.xiangce:
                Intent intent = new Intent(getActivity(), FriendCircle1Activity.class);
                //intent.putExtra("url", "file:///android_asset/friendCircle/index_dex.html");
                startActivity(intent);
                break;
            case R.id.shoucang:

                break;
            case R.id.qianbao:

                break;
            case R.id.kabao:
                Intent intent1 = new Intent(getActivity(), QRCodeActivity.class);
                //intent.putExtra("url", "file:///android_asset/friendCircle/index_dex.html");
                startActivity(intent1);
                break;
            case R.id.biaoqing:

                break;
            case R.id.setting:
                getActivity().startActivity(new Intent(getActivity(), Settings2Fragment.class));
                break;
        }
    }

    /**
     * 弹出选择照片的对话框
     */
    private void showSelectPhotoDialog() {
        List<BaseDialogData> baseDataList = new ArrayList<>();
        baseDataList.add(new BaseDialogData("相机", 0));
        baseDataList.add(new BaseDialogData("图片", 1));
        RecyclerViewDialog recyclerViewDialog = RecyclerViewDialog.getInstance(baseDataList);
        recyclerViewDialog.show(getActivity().getSupportFragmentManager(), "");
        recyclerViewDialog.setOnItemClickListener((position1, view1) -> {
            if (position1 >= 0 && position1 < baseDataList.size()) {
                BaseDialogData baseDialogData = baseDataList.get(position1);
                switch (baseDialogData.getId()) {
                    case 0:
                        BastiGallery.openCamera(getActivity(), baseDialogData.getId());
                        break;
                    case 1:
                        BastiGallery.openGallery(getActivity(), baseDialogData.getId());
                        break;
                }
                recyclerViewDialog.dismiss();
            }
        });
    }


    @Override
    public void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
        if (((MainActivity) getActivity()).isConflict) {
            outState.putBoolean("isConflict", true);
        } else if (((MainActivity) getActivity()).getCurrentAccountRemoved()) {
            outState.putBoolean(Constant.ACCOUNT_REMOVED, true);
        }
    }

    //退出登录
    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onMessageEvent(UpdateAvater event) {
        Glide.with(getActivity()).load(event.getUrl()).into(imaPhoto);
    }

}
